<%@page import="empentity.EmpComponent"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="empsession.EmpSessionComponent"%>
<%@page import="empsession.EmpSessionHome"%>
<%@page import="javax.naming.InitialContext"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

<%!private EmpSessionComponent empSession = null;
	private String ejbsessionname = "ejb/EmpSessionBean";

	public void jspInit() {
		try {
			InitialContext context = new InitialContext();
			EmpSessionHome empSessionHome = (EmpSessionHome) context
					.lookup(ejbsessionname);
			empSession = empSessionHome.create();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}%>
<%
	int empcount = 0;
	String nextpage = request.getParameter("exp") != null ? "ExportServlet.jsp" :
		request.getParameter("del") != null ? "" : "";
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="styles.css" />
<title>JSP Page</title>
</head>
<body>
	<input type="submit" name="add" value="Добавить" onclick="javascript: self.location='addEmp.jsp'">&nbsp
	<form action="ExportServlet.jsp" method="get" name="mainform">
		<input type="submit" name="exp" value="Экспортировать">&nbsp
		<!-- 		<input type="submit" name="del" value="Удалить"> -->
		<table>
			<thead>
				<tr>
					<th></th>
					<th>ФИО</th>
					<th>Должность</th>
					<th>Менеджер</th>
					<th>Дата трудоустройства</th>
					<th>Зарплата</th>
					<th>Комиссионные</th>
					<th>Отдел</th>
				</tr>
			</thead>
			<%
				String findWord = (String) request.getParameter("key");
				String war = (String) request.getParameter("war");
				Collection c = null;
				Iterator iter = null;
				if (war.equals("byName")) {
					c = empSession.getEmpByName(findWord);
				}
				if (war.equals("byJob")) {
					c = empSession.getEmpByJob(findWord);
				}
				iter = c.iterator();
				while (iter.hasNext()) {
					EmpComponent emp = (EmpComponent) iter.next();
			%>
			<tr>
				<td><input type="checkbox" name="emp<%=++empcount%>"
					value="<%=emp.getEmpno()%>"></td>
				<td><a href="viewEmp.jsp?id=<%=emp.getEmpno()%>"><%=emp.getEname()%></a>
				</td>
				<td><%=emp.getJob()%></td>
				<td>
					<%
						if (emp.getMgr() != null) {
					%> <a href="viewEmp.jsp?id=<%=emp.getMgr()%>"> <%=empSession.getEmpByEmpno(emp.getMgr()).getEname()%>
				</a> <%
 	} else {
 %> <%="-"%> <%
 	}
 %>
				</td>
				<td><%=emp.getHiredate()%></td>
				<td><%=emp.getSal()%></td>
				<td>
					<%
						if (emp.getComm() != null) {
					%> <%=emp.getComm()%> <%
 	} else {
 %> <%="-"%> <%
 	}
 %>
				</td>
				<td><a href="viewDept.jsp?id=<%=emp.getDeptno()%>"><%=empSession.getDeptByDeptno(emp.getDeptno())
						.getDname()%></a></td>
			</tr>
			<%
				}
			%>
		</table>
		<input type="hidden" name="count" value="<%=empcount%>">
		<input type="hidden" name="byWhat" value="byEMPNOS">
	</form>
</body>
</html>
